<?php
use app\models\KeyMap;
use app\models\Manager;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $model_list app\models\Manager[]
 * @var $pagination yii\data\Pagination
 */

$this->title = '管理员列表';
$this->params['breadcrumbs'][] = '权限管理';
$this->params['breadcrumbs'][] = $this->title;

app\assets\TableAsset::register($this);
app\assets\AjaxAsset::register($this);
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
    <div class="form-group">
        <label for="search_username" class="sr-only">Username</label>
        <input type="text" class="form-control" name="search_username" id="search_username" placeholder="用户名" value="<?php echo Yii::$app->request->get('search_username');?>" />
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary btn-sm">搜索</button>
    </div>
    <br />
    <div class="form-group">
        <a class="btn btn-success btn-sm" href="<?php echo Url::to(['/admin/rbac/edit-manager']);?>">添加</a>
    </div>
<?php echo Html::endForm();?>
<table id="simple-table" class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th class="center">
                <label class="pos-rel">
                    <input type="checkbox" class="ace" />
                    <span class="lbl"></span>
                </label>
            </th>
            <th>用户名</th>
            <th>手机</th>
            <th>邮箱</th>
            <th>角色</th>
            <th>状态</th>
            <th>创建时间</th>
            <th>操作</th>
        </tr>
    </thead>

    <tbody>
        <?php foreach ($model_list as $model) {?>
            <tr id="data_<?php echo $model->id;?>">
                <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $model->id;?>" /><span class="lbl"><?php echo $model->id;?></span></label></td>
                <td><?php echo Html::a(Html::encode($model->username), '');?></td>
                <td><?php echo Html::encode($model->mobile);?></td>
                <td><?php echo Html::a(Html::encode($model->email), '');?></td>
                <td><?php echo Html::encode($model->role->name);?></td>
                <td><?php echo Html::a(KeyMap::getValue('manager_status', $model->status), 'javascript:void(0)', ['onclick'=>'toggleManagerStatus(' . $model->id . ')', 'class'=>[Manager::STATUS_ACTIVE=>'label label-success', Manager::STATUS_STOPED=>'label label-warning', Manager::STATUS_DELETED=>'label label-error'][$model->status]]);?></td>
                <td><?php echo Yii::$app->formatter->asDatetime($model->create_time);?></td>
                <td><?php echo app\widgets\AdminTableOp::widget(['items'=>[
                    ['icon'=>'fa fa-pencil', 'href'=>Url::to(['/admin/rbac/edit-manager', 'id'=>$model->id]), 'btn_class'=>'btn btn-xs btn-success', 'tip'=>'修改', 'color'=>'green'],
                    ['icon'=>'fa fa-trash', 'onclick'=>'deleteManager(' . $model->id . ')', 'btn_class'=>'btn btn-xs btn-danger', 'tip'=>'删除', 'color'=>'red'],
                ]]);?></td>
            </tr>
        <?php }?>
    </tbody>
</table>
<?php echo app\widgets\LinkPager::widget(['pagination'=>$pagination]);?>
<script>
/**
 * 删除管理员
 */
function deleteManager(id) {
    if (!confirm('确定要删除吗？')) {
        return false;
    }
    $.getJSON('<?php echo Url::to(['/admin/rbac/delete-manager']);?>', {'id':id}, function(json) {
        if (callback(json)) {
            $('#data_' + id).remove();
        }
    });
}
/**
 * 切换管理员状态
 */
function toggleManagerStatus(id) {
    if (!confirm('确定要切换状态吗？')) {
        return false;
    }
    $.getJSON('<?php echo Url::to(['/admin/rbac/set-status']);?>', {'id':id}, function(json) {
        if (callback(json)) {
            window.location.reload();
        }
    });
}
</script>
